﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class XF_SFXXControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        CustomGridView CustomGridList1;
        DropDownList DropDownList1, DropDownList2;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            
            if (!base.Page.IsPostBack)
            {
                QSDataDLL();
                Bind();
            }
            Button btnSeach = (Button)FindControl("btnSeach");
            btnSeach.Click += new EventHandler(Go_Search);
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            string strType = base.Page.Request["type"];
            if (!string.IsNullOrEmpty(strType))
            {
                CustomGridList1.Columns[10].Visible = false;
            }
            if (base.Page.Request["atype"] != null && base.Page.Request["atype"].ToString() == "del")
            {
                bool del = _EluZSDataProvider.DeleteXmByID("ZS_XINFANG", "id", base.Page.Request["id"]);
                if (del) { base.Page.Response.Redirect("XF_SFXX.aspx"); }
            }
        }
        protected void InitControl()
        {
            CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            DropDownList1 = (DropDownList)FindControl("DropDownList1");
            DropDownList2 = (DropDownList)FindControl("DropDownList2");
        }
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Bind()
        {
            CustomGridList1.RowCreated += new GridViewRowEventHandler(CustomGridList1_RowCreated);
            DataTable dt = GetXFXX();
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.DataBind();
        }
        protected DataTable GetXFXX()
        {
            string sql = @"Select (Select 项目名称 From Zs_Project Where Id=t.pnid) 项目名称,
 (Select 立项号 From Zs_Project Where Id=t.pnid) 立项号,
 (Select 区属 From Zs_Project Where Id=t.pnid) 区属,
 (Select 征收实施单位 From Zs_Project Where Id=t.pnid) 征收实施单位,
 t.姓名,t.身份证号,to_char(t.信访时间,'yyyy-MM-dd') 信访时间,t.信访类别,
 t.接待员,t.id zid,t.pnid 
 From Zs_Xinfang t where 1=1 ";
            string sqlWhere = string.Empty;
            TextBox txtZSXKZH = (TextBox)FindControl("txtZSXKZH");
            TextBox txtXMNAME = (TextBox)FindControl("txtXMNAME");
            TextBox txtName = (TextBox)FindControl("txtName");
            TextBox TextBox1 = (TextBox)FindControl("TextBox1");
            TextBox txtDIZHI = (TextBox)FindControl("txtDIZHI");

            DropDownList DropDownList1 = (DropDownList)FindControl("DropDownList1");
            DropDownList DropDownList2 = (DropDownList)FindControl("DropDownList2");

            string strXKZ = txtZSXKZH.Text.Trim();
            string strXName = txtXMNAME.Text.Trim();
            string strName = txtName.Text.Trim();
            string strSfdd = TextBox1.Text.Trim();
            string strDZ = txtDIZHI.Text.Trim();

            string strlb = DropDownList1.SelectedValue;
            string strQS = DropDownList2.SelectedValue;

            string strPID = base.Page.Request["PID"];
            if (!string.IsNullOrEmpty(strXKZ))
            {
                sqlWhere += " and t.pnid in (select pid from zs_prono where 征收许可证号 like '%" + strXKZ + "%')";
            }
            if (!string.IsNullOrEmpty(strXName))
            {
                sqlWhere += " and t.pnid in (select id from zs_project where 项目名称 like '%" + strXName + "%')";
            }
            if (!string.IsNullOrEmpty(strName))
            {
                sqlWhere += " and t.姓名 like '%" + strName + "%'";
            }
            if (!string.IsNullOrEmpty(strlb))
            {
                sqlWhere += " and t.信访类别 like '%" + strlb + "%'";
            }
            if (!string.IsNullOrEmpty(strSfdd))//事发地点
            {
                sqlWhere += " and t.事发地点 like '%" + strSfdd + "%'";
            }
            if (!string.IsNullOrEmpty(strQS))
            {
                if (!_EluZSDataProvider.IsQZSBM(userid))
                {
                    sqlWhere += " and t.pnid in (select id from zs_project where 区属='" + strQS + "')";
                }

            }
            if (!string.IsNullOrEmpty(base.Page.Request["type"]))
            {
                sqlWhere += " and t.区属 ='" + base.Page.Request["type"] + "'";
            }
            if (!string.IsNullOrEmpty(strDZ))
            {
                sqlWhere += " and t.住址 like '%" + strDZ + "%'";
            }
            if (!string.IsNullOrEmpty(strPID))
            {
                sqlWhere += " and t.pnid='" + strPID + "'";
            }
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                string grolename = _EluZSDataProvider.GetString("Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id Where  t.userid Like '%" + userid + "%'");
                if (grolename.IndexOf("玄武区") > -1)
                {
                    sqlWhere += " and t.区属 in('玄武区','白下区')";

                }
                else if (grolename.IndexOf("下关区") > -1)
                {
                    sqlWhere += " and t.区属 in('下关区','鼓楼区')";
                }
                else if (grolename.IndexOf("白下区") > -1)
                {
                    sqlWhere += " and t.区属 in('玄武区','白下区')";

                }
                else if (grolename.IndexOf("鼓楼区") > -1)
                {
                    sqlWhere += " and t.区属 in('下关区','鼓楼区')";
                }
                else
                {
                    sqlWhere += @" and t.区属 in(select keyname from zs_params where name='区属'and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
Where  t.userid Like '%" + userid + "%') Like '%'||keyname||'%')";
                }


            }
            sql += sqlWhere;
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            return dt;
        }

        protected void CustomGridList1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            foreach (GridViewRow gvr in CustomGridList1.Rows)
            {
                if (!string.IsNullOrEmpty(base.Page.Request["type"]))
                {
                    Label lbl = (Label)gvr.FindControl("Label1");
                    lbl.Text = "查看";
                }
            }
        }
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected string GetSqlWhere()
        {
            string sqlWhere = string.Empty;
            TextBox txtZSXKZH = (TextBox)FindControl("txtZSXKZH");
            TextBox txtXMNAME = (TextBox)FindControl("txtXMNAME");
            TextBox txtName = (TextBox)FindControl("txtName");
            TextBox TextBox1 = (TextBox)FindControl("TextBox1");
            TextBox txtDIZHI = (TextBox)FindControl("txtDIZHI");

            DropDownList DropDownList1 = (DropDownList)FindControl("DropDownList1");
            DropDownList DropDownList2 = (DropDownList)FindControl("DropDownList2");

            string strXKZ = txtZSXKZH.Text.Trim();
            string strXName = txtXMNAME.Text.Trim();
            string strName = txtName.Text.Trim();
            string strSfdd = TextBox1.Text.Trim();
            string strDZ = txtDIZHI.Text.Trim();

            string strlb = DropDownList1.SelectedValue;
            string strQS = DropDownList2.SelectedValue;

            string strPID = base.Page.Request["PID"];
            if (!string.IsNullOrEmpty(strXKZ))
            {
                sqlWhere += " and p2.征收许可证号 like '%" + strXKZ + "%'";
            }
            if (!string.IsNullOrEmpty(strXName))
            {
                sqlWhere += " and p1.项目名称 like '%" + strXName + "%'";
            }
            if (!string.IsNullOrEmpty(strName))
            {
                sqlWhere += " and z1.姓名 like '%" + strName + "%'";
            }
            if (!string.IsNullOrEmpty(strlb))
            {
                sqlWhere += " and z1.信访类别 like '%" + strlb + "%'";
            }
            if (!string.IsNullOrEmpty(strSfdd))//事发地点
            {
                sqlWhere += " and z1.事发地点 like '%" + strSfdd + "%'";
            }
            if (!string.IsNullOrEmpty(strQS))
            {
                if (!_EluZSDataProvider.IsQZSBM(userid))
                {
                    sqlWhere += " and p1.区属='" + strQS + "'";
                }

            }
            if (!string.IsNullOrEmpty(base.Page.Request["type"]))
            {
                sqlWhere += " and p1.区属 ='" + base.Page.Request["type"] + "'";
            }
            if (!string.IsNullOrEmpty(strDZ))
            {
                sqlWhere += " and z1.住址 like '%" + strDZ + "%'";
            }
            if (!string.IsNullOrEmpty(strPID))
            {
                sqlWhere += " and p1.id='" + strPID + "'";
            }
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                string sql = "Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id Where  t.userid Like '%" + userid + "%'";
                string g = _EluZSDataProvider.GetString(sql);
                if (g.IndexOf("玄武区") > -1)
                {
                    sqlWhere += " and p1.区属 in('玄武区','白下区')";

                }
                else if (g.IndexOf("下关区") > -1)
                {
                    sqlWhere += " and p1.区属 in('下关区','鼓楼区')";
                }
                else if (g.IndexOf("白下区") > -1)
                {
                    sqlWhere += " and p1.区属 in('玄武区','白下区')";

                }
                else if (g.IndexOf("鼓楼区") > -1)
                {
                    sqlWhere += " and p1.区属 in('下关区','鼓楼区')";
                }
                else
                {
                    sqlWhere += @" and p1.区属 in(select keyname from zs_params where name='区属'and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
Where  t.userid Like '%" + userid + "%') Like '%'||keyname||'%')";
                }


            }
            return sqlWhere;
        }
        protected void QSDataDLL()
        {
            List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("信访类别");
            EluZSProvider.Params item = new EluZSProvider.Params();
            _params.Insert(0, item);
            DropDownList1.DataSource = _params;
            DropDownList1.DataValueField = "keyname";
            DropDownList1.DataTextField = "keyname";
            DropDownList1.DataBind();

            DataTable dt = new DataTable();
            string sql = "Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id Where  t.userid Like '%" + userid + "%'";
            string g = _EluZSDataProvider.GetString(sql);
            if (g == "市征收办(信访科) " || g == "超级管理员")
            {
                List<EluZSProvider.Params> _params1 = _EluZSDataProvider.GetParams("区属");
                _params1.Insert(0, item);
                DropDownList2.DataSource = _params1;
                DropDownList2.DataValueField = "keyname";
                DropDownList2.DataTextField = "keyname";
                DropDownList2.DataBind();
            }
            else
            {
                if (_EluZSDataProvider.IsQZSBM(userid))
                {
                    if (g.IndexOf("玄武区") > -1)
                    {
                        dt = _EluZSDataProvider.GetParamYB("区属", " and keyname='白下区'");

                    }
                    else if (g.IndexOf("下关区") > -1)
                    {
                        dt = _EluZSDataProvider.GetParamYB("区属", " and keyname='鼓楼区'");

                    }
                    else
                    {
                        dt = _EluZSDataProvider.GetParamYB("区属", @" and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id Where  t.userid Like '%" + userid + "%') Like '%'||keyname||'%' ");
                    }
                    DropDownList2.Items.Insert(0, " ");
                    DropDownList2.DataSource = dt;
                    DropDownList2.DataValueField = "keyname";
                    DropDownList2.DataTextField = "keyname";
                    DropDownList2.DataBind();
                }
            }


        }
    }
}


